package com.com.typhoon.config;

import com.com.typhoon.view.CsvView;
import com.com.typhoon.view.ExcelView;
import com.com.typhoon.view.PdfView;
import com.com.typhoon.view.resolver.CsvViewResolver;
import com.com.typhoon.view.resolver.ExcelViewResolver;
import com.com.typhoon.view.resolver.JsonViewResolver;
import com.com.typhoon.view.resolver.PdfViewResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.web.accept.ContentNegotiationManager;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.ContentNegotiatingViewResolver;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

import java.util.*;

/**
 * @author shuya | dongfeng.zdf@alibaba-inc.com
 * @description
 * @date 2018/10/23 下午2:18
 * @description
 * @modified by
 * @since V2.0.0
 */
@Configuration
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {

    @Override
    public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
        Map<String,MediaType> mediaTypes = new HashMap<>();
        //mediaTypes.put("html",MediaType.TEXT_HTML);
       // mediaTypes.put("xml",MediaType.APPLICATION_XML);
        mediaTypes.put("json",MediaType.APPLICATION_JSON);
        mediaTypes.put("excel",MediaType.parseMediaType("application/vnd.ms-excel"));
        mediaTypes.put("csv",MediaType.parseMediaType("text/csv"));
        mediaTypes.put("pdf",MediaType.parseMediaType("application/pdf"));
        configurer.ignoreAcceptHeader(false)
                .favorPathExtension(true)
                .defaultContentType(MediaType.APPLICATION_JSON)
                .favorParameter(true)
                .parameterName("type")
                .mediaTypes(mediaTypes)
        ;
    }

    /*
   * Configure ContentNegotiatingViewResolver
   */
    @Bean
    public ViewResolver contentNegotiatingViewResolver(ContentNegotiationManager manager) {
        ContentNegotiatingViewResolver resolver = new ContentNegotiatingViewResolver();
        resolver.setContentNegotiationManager(manager);
        resolver.setOrder(1);
        // Define all possible view resolvers
        List<ViewResolver> resolvers = new ArrayList<>();
        resolvers.add(csvViewResolver());
        resolvers.add(excelViewResolver());
        resolvers.add(pdfViewResolver());
        resolvers.add(jsonViewResolver());

        resolver.setViewResolvers(resolvers);
        List<View> defaultViews = new ArrayList<>();
        defaultViews.add(new CsvView());
        defaultViews.add(new MappingJackson2JsonView());
        defaultViews.add(new ExcelView());
        defaultViews.add(new PdfView());
        resolver.setDefaultViews(defaultViews);
        return resolver;
    }

    /*
     * Configure View resolver to provide XLS output using Apache POI library to
     * generate XLS output for an object content
     */
    @Bean
    public ViewResolver excelViewResolver() {
        return new ExcelViewResolver();
    }

    /*
     * Configure View resolver to provide Csv output using Super Csv library to
     * generate Csv output for an object content
     */
    @Bean
    public ViewResolver csvViewResolver() {
        return new CsvViewResolver();
    }

    /*
     * Configure View resolver to provide Pdf output using iText library to
     * generate pdf output for an object content
     */
    @Bean
    public ViewResolver pdfViewResolver() {
        return new PdfViewResolver();
    }

    @Bean
    public ViewResolver jsonViewResolver() {
        return new JsonViewResolver();
    }

}
